package cn.lanqiao.system.service;

import cn.lanqiao.system.dao.ProductsMapper;
import cn.lanqiao.system.entity.Products;
import cn.lanqiao.system.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * (Products)对象服务实现类
 *
 * @author david
 * @since 2024-04-09 18:57:38
 */
@Slf4j
@Service
public class ProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Products getProductsById(Integer id) {
        return productsMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过实体作为筛选条件实现模糊查询
     *
     * @param queryVo 查询对象
     * @return 对象列表
     */
    public List<ProductsVo> selectProductsList(QueryProductsVo queryVo){
        log.info("多条件模糊查询参数：{}", queryVo);
        return productsMapper.selectPageList(queryVo);
    }

    /**
     * 新增或修改数据
     *
     * @param updateOrSaveVo 视图对象
     *
     */
    public void insertOrUpdate(ProductsVo updateOrSaveVo) {
        //通过主键是否有值判断是新增还是修改操作
        if(updateOrSaveVo.getId() == null){
            productsMapper.insertSelective(updateOrSaveVo);
        }else{
            productsMapper.updateByPrimaryKeySelective(updateOrSaveVo);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 一个主键或者多个主键 "1" or "1,2,3,4"
     *
     */
    public void deleteByIds(String ids) {
        Example example = new Example(Products.class);
        Example.Criteria criteria = example.createCriteria();

        String[] idArray = ids.split(",");
        if(idArray.length > 0){
            for (String id : idArray) {
                criteria.orEqualTo("id", id);
            }
        }
        productsMapper.deleteByExample(example);
    }
}
